<?php
namespace Apps\AppApi\Method\BeautyParlor;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Models\BeautyParlor;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\BeautyParlorService;
use Apps\Common\Models\CosOnlineAppointment;
use Apps\Common\Libs\SmaUtil;

/**
 *
 * 预约美容院/预约服务
 * @author 545
 * @date     2016年1月13日
 * @version 1.1.2
 * @copyright Copyright 2015 meelier.com
 */
class Appointment extends BaseMethod
{
    public function run(){
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        
        $type = $this->getDataItem('type',1);//预约类型
        $bp_id = intval(trim($this->getDataItem('bp_id',0)));//美容院ID
        $service_id = intval(trim($this->getDataItem('service_id')));//服务id
        $set_man = trim($this->getDataItem('set_man'));//预约人
        $set_time = trim($this->getDataItem('set_time'));//预约时间
        $tel = trim($this->getDataItem('tel'));//联系电话
        
        //验证参数
        if($bp_id == ""||$set_man==""||$set_time==""||$tel=='' || ($type ==2 && $service_id=='')){
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR);
        }
        if ($this->utf8_strlen($set_man)>10){
            return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"预约人不可超过10个字符");
        }
        //预约时间不可小于当前时间 不可大于7天
        if (strtotime($set_time)<time()||strtotime($set_time)>strtotime(' +7 day')){
            return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"预约时间不合法");
        }
        //
        if ($this->utf8_strlen($tel)!=11){
            return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"请输入11位电话号码");
        }
        //验证美容院是否可预约
        $bp_name = BeautyParlor::query()
                ->columns(['bp_name'])
                ->where('bp_id = :bp_id: AND bp_state = 1 ')
                ->bind(['bp_id'=>$bp_id])
                ->execute()->getFirst();
        if (!$bp_name){
            return  $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"美容院不存在");
        }
        
        if ($type ==2){//当预约服务时 验证服务是可用
            $service = BeautyParlorService::query()
                        ->columns('service_name')
                        ->where('service_state>0 and service_is_sell = 1 and beauty_parlor_id=:bid: and service_id = :sid:')
                        ->bind(['bid'=>$bp_id,'sid'=> $service_id])
                        ->execute()->getFirst();
            if (!$service){
                return  $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"服务不可预约");
            }
        }
        
        
        $data['online_product_id'] = $service_id;
        $data['product_name'] = empty($service)?'':$service->service_name;
        $data['appointment_time'] = $set_time;
        $data['set_man'] = $set_man;
        $data['set_man_id'] = $this->getUserAuth()->userId;
        $data['tel'] = $tel;
        $data['set_time'] = date("Y-m-d H:i:s", time());
        $data["appointment_state"] = 0;
        $data["shop_id"] = $bp_id;
        $data["shop_name"] = $bp_name->bp_name;
        $data['type'] = $type;
        

        $appointment = new CosOnlineAppointment();
        
        if($appointment->save($data)){
            $this->id = $appointment->id;
            $this->success = 1;
            
            $branchId = BeautyParlor::findFirst("bp_id = $bp_id")->branch_id;
            if ($branchId){
                //写数据到SMA端
                $smautil = new SmaUtil();
                $smautil->setServiceOrder($branchId, $set_time, $set_man, $tel, $service->service_name);
            }
            return $this->setResult([
                'id'=>$this->id,
                'success'=>$this->success
            ]);
        }else{
            return $this->databaseErrorLog($appointment);
        }
        
    }
}